/*
 * FoodGroupEditor.java
 *
 * Created on August 2, 2006, 8:55 PM
 */

package com.floreantpos.ui.model;

import java.awt.Frame;
import java.util.List;

import com.floreantpos.model.MenuCategory;
import com.floreantpos.model.MenuGroup;
import com.floreantpos.model.dao.MenuCategoryDAO;
import com.floreantpos.model.dao.MenuGroupDAO;
import com.floreantpos.swing.ComboBoxModel;
import com.floreantpos.swing.FixedLengthDocument;
import com.floreantpos.swing.MessageDialog;
import com.floreantpos.ui.BeanEditor;
import com.floreantpos.ui.dialog.BeanEditorDialog;
import com.floreantpos.util.POSUtil;

/**
 *
 * @author  MShahriar
 */
public class MenuGroupForm extends BeanEditor {
    
    /** Creates new form FoodGroupEditor */
	public MenuGroupForm() {
		this(new MenuGroup());
	}
	
    public MenuGroupForm(MenuGroup foodGroup) {
        initComponents();
        
        tfName.setDocument(new FixedLengthDocument(20));
        
        MenuCategoryDAO categoryDAO = new MenuCategoryDAO();
        List<MenuCategory> foodCategories = categoryDAO.findAll();
        cbCategory.setModel(new ComboBoxModel(foodCategories));
        
        setBean(foodGroup);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        tfName = new com.floreantpos.swing.FixedLengthTextField();
        jLabel2 = new javax.swing.JLabel();
        cbCategory = new javax.swing.JComboBox();
        chkVisible = new javax.swing.JCheckBox();
        btnNewCategory = new javax.swing.JButton();

        jLabel1.setText(com.floreantpos.POSConstants.NAME + ":");

        jLabel2.setText(com.floreantpos.POSConstants.CATEGORY + ":");

        chkVisible.setText(com.floreantpos.POSConstants.VISIBLE);
        chkVisible.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        chkVisible.setMargin(new java.awt.Insets(0, 0, 0, 0));

        btnNewCategory.setText("...");
        btnNewCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doNewCategory(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(tfName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                    .add(chkVisible)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(cbCategory, 0, 242, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnNewCategory)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(tfName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(btnNewCategory)
                    .add(cbCategory, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(chkVisible)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void doNewCategory(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doNewCategory
    	try {
			MenuCategoryForm editor = new MenuCategoryForm();
			BeanEditorDialog dialog = new BeanEditorDialog(editor, new Frame(), true);
			dialog.open();
			if (!dialog.isCanceled()) {
				MenuCategory foodCategory = (MenuCategory) editor.getBean();
				ComboBoxModel model = (ComboBoxModel) cbCategory.getModel();
				model.addElement(foodCategory);
				model.setSelectedItem(foodCategory);
			}
		} catch (Exception x) {
			MessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, x);
		}
    }//GEN-LAST:event_doNewCategory
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNewCategory;
    private javax.swing.JComboBox cbCategory;
    private javax.swing.JCheckBox chkVisible;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private com.floreantpos.swing.FixedLengthTextField tfName;
    // End of variables declaration//GEN-END:variables
	@Override
	public boolean save() {
		if(!updateModel()) return false;
		
		MenuGroup foodGroup = (MenuGroup) getBean();
		
		try {
			MenuGroupDAO foodGroupDAO = new MenuGroupDAO();
			foodGroupDAO.saveOrUpdate(foodGroup);
		} catch (Exception e) {
			MessageDialog.showError(e);
			return false;
		}
		return true;
	}

	@Override
	public void dispose() {
	}

	@Override
	protected void updateView() {
		MenuGroup foodGroup = (MenuGroup) getBean();
		if(foodGroup == null) {
			tfName.setText("");
			cbCategory.setSelectedItem(null);
			chkVisible.setSelected(false);
			return;
		}
		tfName.setText(foodGroup.getName());
		chkVisible.setSelected(foodGroup.isVisible());
		
		if(foodGroup.getParent() != null) {
			cbCategory.setSelectedItem(foodGroup.getParent());
		}
	}

	@Override
	protected boolean updateModel() {
		MenuGroup foodGroup = (MenuGroup) getBean();
		if(foodGroup == null) {
			return false;
		}
		
		String name = tfName.getText();
    	if(POSUtil.isBlankOrNull(name)) {
    		MessageDialog.showError("Name is required");
    		return false;
    	}
		
    	MenuCategory category = (MenuCategory) cbCategory.getSelectedItem();
    	if(category == null) {
    		MessageDialog.showError("Category is required");
    		return false;
    	}
    	
		foodGroup.setName(tfName.getText());
		
		foodGroup.setParent(category);
		foodGroup.setVisible(chkVisible.isSelected());
		
		return true;
	}
    
	public String getDisplayText() {
    	MenuGroup foodGroup = (MenuGroup) getBean();
    	if(foodGroup.getId() == null) {
    		return "New menu group";
    	}
    	return "Edit menu group";
    }
}
